package com.mofang.ticketing.common.core.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;


/**
 * 分页结果对象
 *
 * @author LGC
 * @date 2021/7/21 15:01
 */
@ApiModel
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = 2393393283041559398L;

    @ApiModelProperty("列表对象")
    private List<T> data;
    @ApiModelProperty("当前页")
    private long pageNum;
    @ApiModelProperty("当前页条数")
    private long pageSize;
    @ApiModelProperty("总页数")
    private long pageCount;
    @ApiModelProperty("总条数")
    private long total;

    /**
     * @param page
     * @param <T>
     * @return
     */
    public static <T> PageResult<T> getPageResult(IPage<T> page) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setData(page.getRecords());
        pageResult.setPageSize(page.getSize());
        pageResult.setPageCount(page.getPages());
        pageResult.setPageNum(page.getCurrent());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    /**
     * @param data      数据
     * @param pageNum   当前页
     * @param pageSize  当前页条数
     * @param pageCount 总页数
     * @param total     总条数
     * @return 列表对象
     **/
    public static <T> PageResult<T> getPageResult(List<T> data, long pageNum, long pageSize, long pageCount, long total) {
        PageResult<T> result = new PageResult<>();
        result.setData(data);
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setPageCount(pageCount);
        result.setTotal(total);
        return result;
    }

    /**
     * @param data     集合
     * @param pageNum  当前页
     * @param pageSize 每页条数
     * @param total    总条数
     * @return 列表对象
     */
    public static <T> PageResult<T> getPageResult(List<T> data, Integer pageNum, Integer pageSize, Integer total) {
        PageResult<T> result = new PageResult<T>();
        result.setData(data);
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setPageCount((total + pageSize - 1) / pageSize);
        result.setTotal(total);
        return result;
    }


}
